#!/usr/bin/env python
# 
# Copyright 2013 <+YOU OR YOUR COMPANY+>.
# 
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 

from gnuradio import gr, gr_unittest
from gnuradio.digital import digital_swig
import blocks_swig as blocks
from math import sqrt

class qa_qam_snr_estimator (gr_unittest.TestCase):

    def setUp (self):
        self.tb = gr.top_block ()

    def tearDown (self):
        self.tb = None

    def test_001_t (self):
        points = [1+1j, 1-1j, -1+1j, -1-1j]
        pre_diff_code = []
        side = int(sqrt(len(points)))
        width = 2.0/(side-1)

        a = digital_swig.constellation_rect(
            points,         # Constellation points
            pre_diff_code,  # bit map to aply before constellation map
            4,              # Constellation simetry
            side,
            side,
            width,
            width
        )


        src_data = [1+1.5j]*30
        src = gr.vector_source_c (src_data)
        print a
        print a.base()
        snr = blocks.qam_snr_estimator(0, [1,2,3,4], a.base(), [5,6,7], 0)
        self.tb.connect (src, snr)
        self.tb.run ()
        print snr.snr()

if __name__ == '__main__':
    gr_unittest.run(qa_qam_snr_estimator, "qa_qam_snr_estimator.xml")
